package channelEvent

import "time"

type eventList struct {
	ch chan map[string]interface{}
}

func New() *eventList {
	ptr := &eventList{}
	ptr.ch = make(chan map[string]interface{}, 64)
	return ptr
}

func (e *eventList) Close() {
	close(e.ch)
	e.ch = nil
}

func (e *eventList) Notify(message map[string]interface{}) {
	if len(e.ch) == cap(e.ch) {
		return
	}

	e.ch <- message
}

func (e *eventList) HasMessage() bool {
	return len(e.ch) > 0
}

func (e *eventList) Wait(timeout int64) map[string]interface{} {
	select {
	case message := <-e.ch:
		return message
	case <-time.After(time.Duration(timeout) * time.Millisecond):
		return nil
	}
}
